package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.marketBean.MarketComment;
import com.cskaoyan.bean.marketBean.MarketUser;
import com.cskaoyan.bean.vo.zxw.wx.WxCommentCountVo;
import com.cskaoyan.bean.vo.zxw.wx.WxCommentListEntityVo;
import com.cskaoyan.service.wx.WxCommentService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 曾添乐啦
 * @since 2023/01/02 09:36
 */
@RestController
@RequestMapping("wx/comment")
public class WxCommentController {

    @Autowired
    WxCommentService wxCommentService;

    /**
     * `id` int NOT NULL AUTO_INCREMENT,
     * `value_id` int NOT NULL DEFAULT '0' COMMENT '商品id
     * `type` tinyint NOT NULL DEFAULT '0' COMMENT '评论类型，如果type=0，则是商品评论；如果是type=1，则是专题评论；',
     * `content` varchar(1023) DEFAULT '' COMMENT '评论内容',
     * `admin_content` varchar(511) DEFAULT '' COMMENT '管理员回复内容',
     * `user_id` int NOT NULL DEFAULT '0' COMMENT '用户表的用户ID',
     * `has_picture` tinyint(1) DEFAULT '0' COMMENT '是否含有图片',
     * `pic_urls` varchar(1023) DEFAULT NULL COMMENT '图片地址列表，采用JSON数组格式',
     * `star` smallint DEFAULT '1' COMMENT '评分， 1-5',
     * `add_time` datetime DEFAULT NULL COMMENT '创建时间',
     * `update_time` datetime DEFAULT NULL COMMENT '更新时间',
     * `deleted` tinyint(1) DEFAULT '0' COMMENT '逻辑删除',
     * ===========================================================
     * Request URL: http://182.92.235.201:8083/wx/comment/list?valueId=1181051&type=0&limit=20&page=1&showType=0
     * 请求参数
     * valueId=1181051             商品id
     * type=0                     评论类型
     * limit=20
     * page=1
     * showType=0
     *
     * @return
     */
    @RequestMapping("list")
    public BaseRespVo wxCommentList(Integer valueId, Integer type, Integer limit, Integer page, Integer showType) {

        CommonData<WxCommentListEntityVo> commonData = wxCommentService.wxCommentList(valueId, type, limit, page, showType);
        return BaseRespVo.ok(commonData);
    }

    /**
     * 请求参数
     * valueId=1181051
     * type=0
     * Request URL: http://182.92.235.201:8083/wx/comment/count?valueId=1181051&type=0
     * <p>
     * <p>
     * ==============================
     * 响应参数
     * {hasPicCount: 0, allCount: 2}
     *
     * @return
     */
    @RequestMapping("count")
    public BaseRespVo wxCommentCount(Integer valueId, Byte type) {

        WxCommentCountVo wxCommentCountVo = wxCommentService.wxCommentCount(valueId, type);
        return BaseRespVo.ok(wxCommentCountVo);
    }

    /**
     * 请求参数
     * {"type":1,"valueId":"368","content":"不好看的不好看的看习惯了","star":5,"hasPicture":false,"picUrls":[]}
     * content: "1111111111111111111↵22222222222222222↵33333333333333333↵44444444444444444"
     * hasPicture: true
     * picUrls: ["http://182.92.235.201:8083/wx/storage/fetch/mcadwp1aop9p1yrdaldh.jpg"]
     * star: 5
     * type: 1
     * valueId: "368"           商品id
     * ========================================================================================
     * 响应JSON数据
     * addTime: "2023-01-02 17:31:36"
     * content: "1111111111111111111↵22222222222222222↵33333333333333333↵44444444444444444"    √
     * hasPicture: true                                                                        √
     * id: 1111
     * picUrls: ["http://182.92.235.201:8083/wx/storage/fetch/mcadwp1aop9p1yrdaldh.jpg"]        √
     * star: 5                                                                                  √
     * type: 1                                                                                  √
     * updateTime: "2023-01-02 17:31:36"
     * userId: 1                        商品id
     * valueId: 368                                                                              √
     *============================================================================
     * 需要额外封装数据到响应参数中
     *      * addTime: "2023-01-02 17:31:36"
     *      * id: 1111
     *      * updateTime: "2023-01-02 17:31:36"
     *      * userId: 1              在controller层封装
     * @return
     */
    @RequestMapping("post")
    public BaseRespVo wxCommentPost(@RequestBody MarketComment marketComment) {
        //从session域拿到用户信息
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipal();
        Integer userId = marketUser.getId();
        marketComment.setUserId(userId);
        MarketComment marketCommentVo=wxCommentService.wxCommentPost(marketComment);
        return BaseRespVo.ok(marketCommentVo);
    }
}
